package com.haha.keymap;

import com.sun.jna.Native;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef;

/**
 * @author shenlinnan
 * @date 2025/7/6 0:59
 */
public class WindowUtil {

    // 定义Win32 API函数
    public interface CustomUser32 extends User32 {
        CustomUser32 INSTANCE = Native.load("user32", CustomUser32.class);

        int GetWindowTextA(HWND hWnd, byte[] lpString, int nMaxCount);

        HWND GetForegroundWindow();
    }

    // 获取活动窗口标题
    public static String getActiveWindowTitle() {
        byte[] buffer = new byte[1024];
        WinDef.HWND hwnd = CustomUser32.INSTANCE.GetForegroundWindow();
        if (hwnd != null) {
            CustomUser32.INSTANCE.GetWindowTextA(hwnd, buffer, 1024);
            return Native.toString(buffer);
        }
        return "";
    }

}
